extends Node

var config:ConfigFile

## CHUNK WITH BLOCK
var map_uv:Dictionary
var chunk_settings:Dictionary
var section_size:int 
var chunks:Dictionary
var noise:FastNoiseLite=FastNoiseLite.new()


func load_data():
	config = ConfigFile.new()
	var err = config.load("res://data/data_json.txt")
	if err != OK:
		print_debug()
		printerr("File Not Exist!")
	_load_uv()
	_load_chunk()

func generate_init():
	_generate_chunk_init()


func get_block(p_pos:Vector3):
	#未考虑超界情况，当y突破天空及y突破基岩的情况
	var chunk_key = Vector2i(floor(p_pos.x/section_size),floor(p_pos.z/section_size))
	var inner_pos = Vector3(p_pos.x-chunk_key.x*section_size,p_pos.y,p_pos.z-chunk_key.y*section_size)
	return chunks[chunk_key].get_inner_block(inner_pos)

func _load_uv():
	map_uv["path"] = config.get_value("UvMap","path")
	map_uv["div"] = config.get_value("UvMap","div")

func _load_chunk():
	chunk_settings["height"]=config.get_value("chunk_settings","height")
	chunk_settings["chunk_sqrt_count"]=config.get_value("chunk_settings","chunk_sqrt_count")
	section_size=config.get_value("chunk_settings","section_size")
	
func _generate_chunk_init():
	for x in chunk_settings["chunk_sqrt_count"]+2:
		for y in chunk_settings["chunk_sqrt_count"]+2:
			var chunk_key: Vector2i = Vector2i(x,y)
			chunks[chunk_key] = Chunk.new()
			chunks[chunk_key].chunk_pos = chunk_key
			chunks[chunk_key].world_pos = chunk_key*section_size
			chunks[chunk_key].generate_init()
	
